﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BDCS.Model;
using BDCS.BLL;
namespace BDCS.UI
{
    public partial class AddBuildDesignDcriterionContents : Form
    {
        #region 成员字段
        private BLL.T_BuildDesignDcriterionContents bllBuildDesignDcriterionContent;
        private BLL.T_BuildDesignDcriterionSubTypes bllBuildDesignDcriterionSubType;
        private Model.T_BuildDesignDcriterionContents modelBuildDesignDcriterionContent;
        private List<Model.T_BuildDesignDcriterionContents> listBuildDesignDcriterionContent;
        private DataSet dataSet;
        private bool isExist;
        #endregion
        #region 构造函数
        public AddBuildDesignDcriterionContents()
        {
            InitializeComponent();
        } 
        #endregion
        #region 成员事件
        private void AddBuildDesignDcriterionContents_Load(object sender, EventArgs e)
        {
            DataBinding();
        }
        private void btnOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(rtbBuildDesignDcriterionContent.Text.Trim()))
            {
                MessageBox.Show("请输入规则内容！");
            }
            else
            {
                if (string.IsNullOrEmpty(txtRule.Text.Trim()))
                {
                    MessageBox.Show("请输入规则内容所属规定名称！");
                }
                else
                {
                    if (rtbBuildDesignDcriterionContent.Text.Length > 100 && txtRule.Text.Length > 100)
                    {
                        MessageBox.Show("输入内容长度过长，超过100字符串，请重新输入！");
                    }
                    else
                    {
                        modelBuildDesignDcriterionContent = new Model.T_BuildDesignDcriterionContents();
                        modelBuildDesignDcriterionContent.DSTID = Convert.ToInt32(cobSubBuildName.SelectedValue);
                        modelBuildDesignDcriterionContent.DcriterionContent = rtbBuildDesignDcriterionContent.Text.Trim();
                        modelBuildDesignDcriterionContent.DcriterionRules = txtRule.Text.Trim();
                        if (!CheckAttributeIsExist(modelBuildDesignDcriterionContent))
                        {
                            bllBuildDesignDcriterionContent = new BLL.T_BuildDesignDcriterionContents();
                            if (bllBuildDesignDcriterionContent.Add(modelBuildDesignDcriterionContent) != 0)
                            {
                               // BDCS.Common.TreeViewState.IsChangedDcriterion = true;
                                MessageBox.Show("新增规范内容成功！");
                            }
                            else
                            {
                                MessageBox.Show("新增规范内容失败，请重新输入!");
                                txtRule.Text = string.Empty;
                                rtbBuildDesignDcriterionContent.Text = string.Empty;
                            }
                        }
                        else
                        {
                            MessageBox.Show("新增规范内容已经存在，请重新输入！");
                            txtRule.Text = string.Empty;
                            rtbBuildDesignDcriterionContent.Text = string.Empty;
                        }
                    }
                }
            }
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            rtbBuildDesignDcriterionContent.Text = string.Empty;
            txtRule.Text = string.Empty;
        } 
        #endregion
        #region 成员方法
        private void DataBinding()
        {
            dataSet = new DataSet();
            bllBuildDesignDcriterionSubType = new BLL.T_BuildDesignDcriterionSubTypes();
            dataSet = bllBuildDesignDcriterionSubType.GetAllList();
            cobSubBuildName.DataSource = dataSet.Tables[0];
            cobSubBuildName.DisplayMember = dataSet.Tables[0].Columns["DSTName"].ToString();
            cobSubBuildName.ValueMember = dataSet.Tables[0].Columns["DSTID"].ToString();
        }
        private bool CheckAttributeIsExist(Model.T_BuildDesignDcriterionContents modelDesignDcriterionContent)
        {
            listBuildDesignDcriterionContent = new List<Model.T_BuildDesignDcriterionContents>();
            bllBuildDesignDcriterionContent = new BLL.T_BuildDesignDcriterionContents();
            listBuildDesignDcriterionContent = bllBuildDesignDcriterionContent.GetModelList("DSTID=" + "'" + modelDesignDcriterionContent.DSTID.ToString() + "'");
            if (listBuildDesignDcriterionContent.Count != 0)
            {
                foreach (Model.T_BuildDesignDcriterionContents BuildDesignDcriterionContent in listBuildDesignDcriterionContent)
                {
                    if (modelDesignDcriterionContent.DSTID == BuildDesignDcriterionContent.DSTID && modelDesignDcriterionContent.DcriterionContent == BuildDesignDcriterionContent.DcriterionContent)
                    {
                        isExist = true;
                    }
                    else
                    {
                        isExist = false;
                    }
                }
            }
            else
            {
                isExist = false;
            }
            return isExist;
        } 
        #endregion
    }
}
